package com.smalls.newvideotwo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.Timer;
import java.util.TimerTask;

public class SmoothScrollListView extends ListView implements OnKeyListener {
	private ListAdapter adapter;
	public boolean isScrollTop;
	public int itemHeight;
	public int itemsCount;
	public OnScrollBottomListener onScrollBottomListener;
	public OnScrollTopListener onScrollTopListener;
	public int scrollDuration = 500;
	public Timer timer;

	public interface OnScrollBottomListener {
		void onScrollBottom();
	}

	public interface OnScrollTopListener {
		void onScrollTop();
	}

	public SmoothScrollListView(Context context) {
		super(context);
	}

	public SmoothScrollListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public SmoothScrollListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOnKeyListener(this);
		setSmoothScrollbarEnabled(true);
	}

	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		if (!(this.adapter == null || this.adapter.isEmpty())) {
			if (getChildAt(0) != null) {
				this.itemHeight = getChildAt(0).getHeight();
			} else {
				this.itemHeight = 98;
			}
		}
		super.onLayout(changed, l, t, r, b);
	}

	public void setAdapter(ListAdapter adapter) {
		super.setAdapter(adapter);
		this.adapter = adapter;
		this.itemsCount = adapter.getCount();
	}

	public void setScrollDuration(int scrollDuration) {
		this.scrollDuration = scrollDuration;
	}

	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_UP) {
			return false;
		}
		int correntItemPosition = getSelectedItemPosition();
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:
			Log.i("hs", "SmoothScrollListView KEYCODE_DPAD_UP");
			if (correntItemPosition == 1) {
				if (getChildAt(0).isFocused()) {
					return false;
				}
				smoothScrollBy(-this.itemHeight, this.scrollDuration);
				return false;
			} else if (correntItemPosition != 0) {
				smoothScrollBy(-this.itemHeight, this.scrollDuration);
				return false;
			} else if (this.onScrollTopListener == null) {
				return false;
			} else {
				this.onScrollTopListener.onScrollTop();
				return this.isScrollTop;
			}
		case KeyEvent.KEYCODE_DPAD_DOWN:
			Log.i("hs", "SmoothScrollListView KEYCODE_DPAD_DOWN");
			if (correntItemPosition == this.itemsCount - 2) {
				if (correntItemPosition != this.itemsCount - 1 || this.timer == null) {
					return false;
				}
				smoothScrollBy(this.itemHeight, this.scrollDuration);
				this.timer.cancel();
				this.timer = null;
				smoothScrollToButtom();
				return false;
			} else if (correntItemPosition == this.itemsCount - 1) {
				if (this.onScrollBottomListener != null) {
					this.onScrollBottomListener.onScrollBottom();
				}
				return true;
			} else {
				smoothScrollBy(this.itemHeight, this.scrollDuration);
				return false;
			}
		default:
			return false;
		}
	}

	public void smoothScrollToButtom() {
		this.timer = new Timer();
		this.timer.schedule(new TimerTask() {
			public void run() {
				SmoothScrollListView.this.setSelection(SmoothScrollListView.this
						.getLastVisiblePosition());
			}
		}, (long) (this.scrollDuration / 3));
	}

	public void setOnScrollBottomListener(OnScrollBottomListener onScrollBottomListener) {
		this.onScrollBottomListener = onScrollBottomListener;
	}

	public void setOnScrollTopListenrt(OnScrollTopListener onScrollTopListener) {
		this.onScrollTopListener = onScrollTopListener;
		this.isScrollTop = true;
	}
}
